<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="公司" prop="company.id"
                :rules="[{required: true, message:'公司不能为空', trigger:'blur'}]">
                <el-select v-model="inputForm.company.id" placeholder="请选择"
                  style="width: 100%">
                  <el-option v-for="(item, index) in companys" :label="item.name"
                    :key="index" :value="item.id"></el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="部门" prop="office.id"
                :rules="[{required: true, message:'部门不能为空', trigger:'blur'}]">
              <el-select v-model="inputForm.office.id" placeholder="请选择"
                  style="width: 100%">
                  <el-option v-for="(item, index) in offices" :label="item.name"
                    :key="index" :value="item.id"></el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="创建人" prop="founder.id"
                :rules="[{required: true, message:'创建人不能为空', trigger:'blur'}]">
          <el-input v-model="userName" placeholder="" disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="通知标题" prop="noticetitle"
                :rules="[{required: true, message:'通知标题不能为空', trigger:'blur'}]">
              <el-input v-model="inputForm.noticetitle" placeholder="请填写通知标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否为红头文件" prop="isred"
                :rules="[{required: true, message:'是否为红头文件不能为空', trigger:'blur'}]">
                <el-select v-model="inputForm.isred" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.isred!=''&&inputForm.isred==0">
            <el-form-item label="主题词" prop="subjecthead"
                :rules="[]">
              <el-input v-model="inputForm.subjecthead" placeholder="请填写主题词"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="inputForm.isred!=''&&inputForm.isred==0">
            <el-form-item label="抄送人" prop="person"
                :rules="[ ]
                ">
              <!-- <el-input v-model="inputForm.person" placeholder="请填写抄送人"     ></el-input> -->
               <el-select  v-model="findListValue" multiple filterable  allow-create default-first-option placeholder="请选择文章标签">
                <el-option v-for="item in findList" :key="item.id" :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
           </el-form-item>
        </el-col>
        
        <el-col :span="24">
            <el-form-item label="通知内容" prop="noticecontent"
                :rules="[{required: true, message:'通知内容不能为空', trigger:'blur'}]">
            <WangEditor ref="noticecontentEditor" v-model="inputForm.noticecontent"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        userName: '',
        inputForm: {
          id: '',
          company: {
            id: ''
          },
          office: {
            id: ''
          },
          founder: {
            id: ''
          },
          noticetitle: '',
          number: '',
          noticecontent: '',
          seeuser: '',
          companyType: '',
          isred: '',
          subjecthead: '',
          person: '',
          remarks: '',
          companyType:this.$route.query.companyType,
        },
        companys:[],
        offices:[],
        findList:[],
        findListValue:[]
      }
    },
    components: {
      GridSelect,
      WangEditor
    },
    activated () {
      this.getCompanys();
      this.departments();
      this.getFindList();
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建通知公告`
        } else if (method === 'edit') {
          this.title = '修改通知公告'
        } else if (method === 'view') {
          this.title = '查看通知公告'
        }
        this.inputForm.founder.id = this.$store.state.user.id;
        this.userName = this.$store.state.user.name;
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.noticecontentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/companynotice/companyNotice/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.companyNotice)
              this.$refs.noticecontentEditor.init(this.inputForm.noticecontent)
              this.findListValue=this.inputForm.person.split(',');
              this.loading = false
            })
          }
        })
      },
      getFindList(){
        this.$http({
            url: "/app/sys/user/findList",
            method: "get",
          }).then(({
            data
          }) => {
            if (data) {
              this.findList = data.page.list;
            }
          });
      },
        getCompanys() {
          this.companys = [];
          this.$http({
            url: "/sys/web/userinfo/findCompany",
            method: "get",
            params: {
              id: this.$store.state.user.companyId
            },
          }).then(({
            data
          }) => {
            if (data) {
              this.companys = data.list;
            }
          });
        },
        departments() {
          this.offices = [];
          this.$http({
            url: "/sys/web/userinfo/findDepartemnt",
            method: "get",
            params: {
              id: this.$store.state.user.companyId
            },
          }).then(({
            data
          }) => {
            if (data) {
              this.offices = data.list;
            }
          });
        },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.inputForm.person=this.findListValue.join(",");
            this.$http({
              url: `/dlyrl/hr/companynotice/companyNotice/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>